package com.tk.config;

import ch.qos.logback.core.db.DBHelper;
import com.tk.bean.Car;
import com.tk.bean.Pet;
import com.tk.bean.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * 默认单实例
 * 本身也是组件
 * proxyBeanMethods:代理 bean 方法
 *   Full: (proxyBeanMethods = true)
 *   Lite: (proxyBeanMethods = false)   bean方法被别的bean方法调用
 *
 *   @ImportResource("classpath:bean.xml") 导入Spring 配置文件
 */
@Import({User.class, DBHelper.class}) // 默认组件名：全类名
@Configuration(proxyBeanMethods = false)
@ImportResource("classpath:bean.xml")
@EnableConfigurationProperties(Car.class)  //第二种方法
public class MyConfig {

    /**
     * 外部无论对配置类中的这个组件注册方法调用多少次，获取的都是之前注册容器中的单实例
     * @return
     */
    @Bean
    public User user01(){
        User zhangsan = new User("张三", 18);
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }

    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}
